package com.item.yxlottery.yxlottery.activity;

/*
 *------------------------------
 * @创建者      Administrator
 * @创建时间    2017/1/4 15:44
 * @文件描述    具体的彩店详情
 *------------------------------
 */

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.item.yxlottery.yxlottery.R;
import com.item.yxlottery.yxlottery.adapter.StoreLotteryAdapter;
import com.item.yxlottery.yxlottery.base.BaseActivity;
import com.item.yxlottery.yxlottery.bean.StoreBean;
import com.item.yxlottery.yxlottery.callback.MyStringCallBack;
import com.item.yxlottery.yxlottery.common.Constant;
import com.item.yxlottery.yxlottery.utils.UrlUtil;
import com.item.yxlottery.yxlottery.views.DividerItemDecoration;
import com.item.yxlottery.yxlottery.views.SimpleRatingBar;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

import static com.item.yxlottery.yxlottery.views.DividerItemDecoration.VERTICAL_LIST;

public class StoreDetailActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_storedetail_image)
    ImageView mIvImage;//照片

    @Bind(R.id.tv_storedetail_name)
    TextView mTvName;//名称

    @Bind(R.id.tv_storedetail_address)
    TextView mTvAddress;//地址

    @Bind(R.id.tv_storedetail_distance)
    TextView mTvDistance;//距离

    @Bind(R.id.srb_storedetail_star)
    SimpleRatingBar mSrbStar;//评分条

    @Bind(R.id.tv_storedetail_score)
    TextView mTvScore;//具体评分

    @Bind(R.id.tv_storedetail_sales)
    TextView mTvSales;//销量

    @Bind(R.id.tv_storedetail_comments)
    TextView mTvComments;//评论

    @Bind(R.id.rv_storedetail_list)
    RecyclerView mRvList;

    private List<JSONObject> mLottery = new ArrayList<>();
    private StoreLotteryAdapter mAdapter;

    @Override
    protected void setCustomActionBar(ImageButton ibActionbarLeft, TextView tvActionBarTitle, ImageButton ibActionbarRight, AutoRelativeLayout layoutActionbar) {
        ibActionbarLeft.setImageResource(R.drawable.leftarrow);
        ibActionbarLeft.setOnClickListener(this);
        ibActionbarRight.setImageResource(R.drawable.star);
        ibActionbarRight.setOnClickListener(this);
        tvActionBarTitle.setText("彩票店");
    }

    @Override
    protected View initView() {
        View view = View.inflate(this, R.layout.activity_storedetail, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        setRecycelViewData();
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("store")) {
            StoreBean store = intent.getParcelableExtra("store");
            mTvName.setText(store.getStoreName());
            mTvDistance.setText("距您" + new DecimalFormat("#.00").format(Double.parseDouble(store.getDistance()) / 1000) + "公里");
            OkHttpUtils.post().url(UrlUtil.getStoreInfoUrl()).addParams("storeId", "36")//intent.getStringExtra("storeId")
                    .build().execute(new MyStringCallBack(this, true) {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {
                    JSONObject parse = (JSONObject) JSON.parse(response);
                    if (parse.getString("error_code").equals(Constant.SUCCESS)) {
                        JSONObject attrs = (JSONObject) JSON.parse(parse.getString("attrs"));
                        JSONObject store = (JSONObject) JSON.parse(attrs.getString("store"));//彩店评分,销量,图片信息
                        Glide.with(StoreDetailActivity.this).load((UrlUtil.getImageHomeUrl() + store.getString("storeBanner"))).into(mIvImage);
                        mTvAddress.setText(store.getString("address"));
                        mTvSales.setText("销量: " + store.getString("sales") + "单");
                        String score = store.getString("score");
                        score = "3.6";
                        mSrbStar.setRating(Float.parseFloat(score));
                        mTvScore.setText(score);
                        mTvComments.setText(store.getString("commentCount") + "条评论");
                        JSONArray lottery = JSON.parseArray(attrs.getString("lottery"));//可售彩种信息
                        for (Object obj : lottery) {
                            mLottery.add((JSONObject) obj);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }

    @Override
    protected void initEvent() {
        mTvComments.setOnClickListener(this);
        mRvList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(StoreDetailActivity.this,mLottery.get(position).getString("goodsName"),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_actionbar_left:
                finish();
                break;
            case R.id.ib_actionbar_right://加关注

                break;
            case R.id.tv_storedetail_comments://评论
                startActivity(new Intent(this, StoreCommentsActivity.class));
                break;
        }
    }

    private void setRecycelViewData() {
        mAdapter = new StoreLotteryAdapter(this, R.layout.item_activity_storedetail_list, mLottery);
        mAdapter.openLoadAnimation();//开启默认渐显动画
        mRvList.setLayoutManager(new GridLayoutManager(this, 4));
        mRvList.addItemDecoration(new DividerItemDecoration(this, VERTICAL_LIST, 0xffC3C3C3, 1));//分割线
        mRvList.setAdapter(mAdapter);
    }
}
